package cn.signalpha.modules.shop.wrapper;

import cn.signalpha.core.mp.support.BaseEntityWrapper;
import cn.signalpha.core.tool.constant.SystemConstant;
import cn.signalpha.core.tool.node.ForestNodeMerger;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.shop.cache.SellerSysCache;
import cn.signalpha.modules.shop.entity.SellerMenu;
import cn.signalpha.modules.shop.vo.SellerMenuVO;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author signalpha
 */
public class SellerMenuWrapper extends BaseEntityWrapper<SellerMenu, SellerMenuVO>  {

	public static SellerMenuWrapper build() {
		return new SellerMenuWrapper();
 	}

	@Override
	public SellerMenuVO entityVO(SellerMenu sellerMenu) {
		SellerMenuVO menuVO = Objects.requireNonNull(BeanUtil.copy(sellerMenu, SellerMenuVO.class));
		if (Func.equals(sellerMenu.getParentId(), SystemConstant.TOP_PARENT_ID)) {
			menuVO.setParentName(SystemConstant.TOP_PARENT_NAME);
		} else {
			SellerMenu parent = SellerSysCache.getMenu(sellerMenu.getParentId());
			menuVO.setParentName(parent.getName());
		}
		return menuVO;
	}

	public List<SellerMenuVO> listNodeVO(List<SellerMenu> list) {
		List<SellerMenuVO> collect = list.stream().map(menu -> BeanUtil.copy(menu, SellerMenuVO.class)).collect(Collectors.toList());
		return ForestNodeMerger.merge(collect);
	}

	public List<SellerMenuVO> listNodeLazyVO(List<SellerMenuVO> list) {
		return ForestNodeMerger.merge(list);
	}

}
